/*
	Copyright (C) 2008 Demiurg-HG
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
// ui_menu.cpp - 

#error this file should be excluded from project

#include "ui_local.h"
#include "../library/tixml_helper.h"


//
//	EUIMenuLoader::EUIMenuLoader
//
EUIMenuLoader::EUIMenuLoader(EUserInterface *ui, const char *path)
{
	this->ui = ui;
	fs  = ui->fs;
	con = ui->con;
	

	LOG_ACTION(va("Loading UI : %s", path));
	
	try {

		IFile	*f = fs->FileOpen(path, FS_OPEN_READ);
		vector<char> text;
		text.resize(f->Size()+1, '\0');
		f->Read(&text[0], f->Size());
		fs->FileClose(f);
		
		TiXmlDocument	xmldoc;
		xmldoc.Parse(&text[0]);
		
		if (xmldoc.Error()) {
			RAISE_EXCEPTION(va("XML Error : %s(%d, %d) : %s", path, 
						xmldoc.ErrorRow(), xmldoc.ErrorCol(), xmldoc.ErrorDesc()));
		}
		
		TiXmlElement	*root = xmldoc.RootElement();
		
		if ( !root || strcmp(root->Value(), "menu")!=0 ) {
			RAISE_EXCEPTION("<menu> section not found");
		}
		
		//
		//	Enumerate <frame> :
		//
		TiXmlElement *xml_frame = NULL;
		for ( xml_frame = (TiXmlElement*)root->FirstChild("frame"); xml_frame; xml_frame = xml_frame->NextSiblingElement("frame") )
		{
			if (!xml_frame) break;

			string name = "no_name";
			
			XML_LoadAttribute(xml_frame, "name", &name);
		
			EUIFrame	*frame = new EUIFrame(ui, name.c_str());

			ui->RegisterUIObject(frame);
						
			frames.push_back(frame);
			
			LoadControlInfo(xml_frame, frame);
		}
		
		
		
	} catch ( exception &e )	
	{
		LOG_FAILED();
		
		LOGF("%s\r\n", e.what());
		
		con->DropWarnings();
		return;
	}
	
	LOG_OK();
	con->DropWarnings();
}


//
//	EUIMenuLoader::~EUIMenuLoader
//
EUIMenuLoader::~EUIMenuLoader(void)
{
}


//
//	EUIMenuLoader::LoadControlInfo
//
void EUIMenuLoader::LoadControlInfo(TiXmlElement *xml_frame, EUIFrame *frame)
{
	TiXmlElement *xml_ctrl;
	for ( xml_ctrl = (TiXmlElement*)xml_frame->FirstChild("control"); xml_ctrl; xml_ctrl = xml_ctrl->NextSiblingElement("control") )
	{
		if (!xml_ctrl) break;

		const char *class_name = xml_ctrl->Attribute("class");
		if (class_name) {
		
			int x,y,w,h;
			string command="", text="";
			
			//
			//	Load Common Attributes :
			//
			XML_LoadAttribute(xml_ctrl, "x", &x);
			XML_LoadAttribute(xml_ctrl, "y", &y);
			XML_LoadAttribute(xml_ctrl, "w", &w);
			XML_LoadAttribute(xml_ctrl, "h", &h);
			XML_LoadAttribute(xml_ctrl, "text", &text);
			XML_LoadAttribute(xml_ctrl, "command", &command);
			
			EUIControl *control = NULL;			
			
			//
			//	BUTTON :
			//
			if (strcmp(class_name, "button")==0) {
				control = new EUIFrameButton(ui, text.c_str(), command.c_str());
			}

			if (strcmp(class_name, "image")==0) {
				string image_name;
				XML_LoadAttribute(xml_ctrl, "image", &image_name);
				control = new EUIFrameImage(ui, image_name.c_str());
			}
			
			if (control) {
				control->SetRect(uiRect_s(x,y,w,h));
				frame->AttachControl(control);
			}else {
				WARNING(va("Control \"%s\" not created", class_name));
			}
			
		}
	}
}









